

// always requires an idle view 
#include "kb.h"
#include "touch_algorithm.h"
#include "custom_event.h"
#include "gui.h"

#include "views.h"

#include "xapp_access.h"
#include "xapp_profile.h"
#include "xapp_localize.h"


enum tagLanguageSelect
{
    tagLanguageSelectEnglish,
    tagLanguageSelectSimplifyChinese,
    #if !defined(ONLY_EN_CH_LANGUAGE)
    tagLanguageSelectTraditionalChinese,
    tagLanguageSelectJapanese,
    tagLanguageSelectSpanish,
    tagLanguageSelectPorturgues,
    tagLanguageSelectFranch,
    tagLanguageSelectGerman,
    tagLanguageSelectRussian,
    tagLanguageSelectItaly,
    tagLanguageSelectPolish,
    #endif
    tagLanguageSelectCount,
};

enum tagLanguage
{
    tagLanguageItem0,
    tagLanguageItem1,
    tagLanguageItem2,
    tagLanguageItem3,
    tagLanguageItem4,
    tagLanguageItemCount,
    tagLanguageIgnore,

    tagLanguageBack,
};


typedef struct
{
    uint16_t origin; // 原始文本
    uint16_t local; // 当前文本描述    
    uint16_t tag;
}language_item_t;


GUI_VIEW_FUNC_DECLARE_ALL;
static void local_view_event_handler(uint32_t tag);

static const language_item_t LANGUAGE_ITEMS[] =
{
    {.origin = TEXT_TAG_LANGUAGES_ENGLISH, .local = TEXT_TAG_LANGUAGES_LOCAL_ENGLISH, .tag = tagLanguageSelectEnglish},
    {.origin = TEXT_TAG_LANGUAGES_SIMPLIFY_CHINESE, .local = TEXT_TAG_LANGUAGES_LOCAL_SIMPLIFY_CHINESE, .tag = tagLanguageSelectSimplifyChinese },
    #if !defined(ONLY_EN_CH_LANGUAGE)
    {.origin = TEXT_TAG_LANGUAGES_TRANDITIONAL_CHINESE, .local = TEXT_TAG_LANGUAGES_LOCAL_TRANDITIONAL_CHINESE, .tag = tagLanguageSelectTraditionalChinese },
    {.origin = TEXT_TAG_LANGUAGES_JAPANESE, .local = TEXT_TAG_LANGUAGES_LOCAL_JAPANESE, .tag = tagLanguageSelectJapanese },
    {.origin = TEXT_TAG_LANGUAGES_SPANISH, .local = TEXT_TAG_LANGUAGES_LOCAL_SPANISH, .tag = tagLanguageSelectSpanish },
    {.origin = TEXT_TAG_LANGUAGES_PORTURGUES, .local = TEXT_TAG_LANGUAGES_LOCAL_PORTURGUES, .tag = tagLanguageSelectPorturgues },
    {.origin = TEXT_TAG_LANGUAGES_FRANCH, .local = TEXT_TAG_LANGUAGES_LOCAL_FRANCH, .tag = tagLanguageSelectFranch },
    {.origin = TEXT_TAG_LANGUAGES_GERMAN, .local = TEXT_TAG_LANGUAGES_LOCAL_GERMAN, .tag = tagLanguageSelectGerman },
    {.origin = TEXT_TAG_LANGUAGES_RUSSIAN, .local = TEXT_TAG_LANGUAGES_LOCAL_RUSSIAN, .tag = tagLanguageSelectRussian },
    {.origin = TEXT_TAG_LANGUAGES_ITALY, .local = TEXT_TAG_LANGUAGES_LOCAL_ITALY, .tag = tagLanguageSelectItaly },
    {.origin = TEXT_TAG_LANGUAGES_POLISH, .local = TEXT_TAG_LANGUAGES_LOCAL_POLISH, .tag = tagLanguageSelectPolish },
    #endif

};

#define ITEM_HEIGHT (GUI_LCD_RES_V / 4)

#define ROLL_V_MAX_OFFSET   (1*ITEM_HEIGHT)
#define ROLL_V_MIN_OFFSET   (-(ITEM_HEIGHT * (tagLanguageSelectCount - 2)))

#define SUB_TEXT_COLOR      0x998D

const static ui_widgets_type_t wglLanguage[] =
{
    [0] = uiCanvas(0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK),

    [1] = uiListBoxTouch(0,0,GUI_LCD_RES_H, GUI_LCD_RES_V, 20, 0, 0, false, tagLanguageIgnore),

    [2] = uiStackTouch(0, ITEM_HEIGHT * (-1), GUI_LCD_RES_H, ITEM_HEIGHT, 3, tagLanguageItem0),
    //[3] = uiRectangleShadow( 16, 0, GUI_LCD_RES_H - 32, ITEM_HEIGHT - 4, COLOR_BLACK, COLOR_WHITE, false ),
    [3] = uiCanvas( 0, ITEM_HEIGHT - 2, GUI_LCD_RES_H, 2, COLOR_WHITE ),
    [4] = uiTextbox( 20, 4, GUI_LCD_RES_H - 40, ITEM_HEIGHT / 2, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),
    [5] = uiTextbox( 30, 40, GUI_LCD_RES_H - 40, ITEM_HEIGHT / 2, COLOR_BLACK, SUB_TEXT_COLOR, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),

    [6] = uiStackTouch(0, ITEM_HEIGHT * 0, GUI_LCD_RES_H, ITEM_HEIGHT, 3, tagLanguageItem1),
    //[7] = uiRectangleShadow(16, 0, GUI_LCD_RES_H - 32, ITEM_HEIGHT - 4, COLOR_BLACK, COLOR_WHITE, false),
    [7] = uiCanvas(0, ITEM_HEIGHT - 2, GUI_LCD_RES_H, 2, COLOR_WHITE),
    [8] = uiTextbox(20, 4, GUI_LCD_RES_H - 40, ITEM_HEIGHT / 2, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29),
    [9] = uiTextbox(30, 40, GUI_LCD_RES_H - 40, ITEM_HEIGHT / 2, COLOR_BLACK, SUB_TEXT_COLOR, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29),

    [10] = uiStackTouch(0, ITEM_HEIGHT, GUI_LCD_RES_H, ITEM_HEIGHT, 3, tagLanguageItem2),
    //[11] = uiRectangleShadow(16, 0, GUI_LCD_RES_H - 32, ITEM_HEIGHT - 4, COLOR_BLACK, COLOR_WHITE, false),
    [11] = uiCanvas(0, ITEM_HEIGHT - 2, GUI_LCD_RES_H, 2, COLOR_WHITE),
    [12] = uiTextbox(20, 4, GUI_LCD_RES_H - 40, ITEM_HEIGHT / 2, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29),
    [13] = uiTextbox(30, 40, GUI_LCD_RES_H - 40, ITEM_HEIGHT / 2, COLOR_BLACK, SUB_TEXT_COLOR, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29),

    [14] = uiStackTouch(0, ITEM_HEIGHT * 2, GUI_LCD_RES_H, ITEM_HEIGHT, 3, tagLanguageItem3),
    //[15] = uiRectangleShadow(16, 0, GUI_LCD_RES_H - 32, ITEM_HEIGHT - 4, COLOR_BLACK, COLOR_WHITE, false),
    [15] = uiCanvas(0, ITEM_HEIGHT - 2, GUI_LCD_RES_H, 2, COLOR_WHITE),
    [16] = uiTextbox(20, 4, GUI_LCD_RES_H - 40, ITEM_HEIGHT / 2, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29),
    [17] = uiTextbox(30, 40, GUI_LCD_RES_H - 40, ITEM_HEIGHT / 2, COLOR_BLACK, SUB_TEXT_COLOR, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29),

    [18] = uiStackTouch(0, ITEM_HEIGHT * 3, GUI_LCD_RES_H, ITEM_HEIGHT, 3, tagLanguageItem4),
    //[19] = uiRectangleShadow(16, 0, GUI_LCD_RES_H - 32, ITEM_HEIGHT - 4, COLOR_BLACK, COLOR_WHITE, false),
    [19] = uiCanvas(0, ITEM_HEIGHT - 2, GUI_LCD_RES_H, 2, COLOR_WHITE),
    [20] = uiTextbox(20, 4, GUI_LCD_RES_H - 40, ITEM_HEIGHT / 2, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29),
    [21] = uiTextbox(30, 40, GUI_LCD_RES_H - 40, ITEM_HEIGHT / 2, COLOR_BLACK, SUB_TEXT_COLOR, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29),

    [22] = uiPictureBoxOverlay( IMG_Exercise_Play_Stop_Button_075_075_02, 1, 180, 0, SIZE_SCALE(50), SIZE_SCALE(50), COLOR_BLACK ),
};


GUI_VIEW_DEF_NR(viewWatch_Language) =
{
    // name 
    .name = "Watch+Language",

    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func,

    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,

    .pWidgetlist = (ui_widgets_type_t*)wglLanguage ,
    .widget_cnt = WIDGET_COUNT(wglLanguage),

    .flag = GUI_VIEW_FLAG_NONE
};

static gui_fling_t* mp_lang_fling;
static int16_t      m_idx_of_first_item = -1;
static int16_t      m_idx_of_selected = 0;

/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0
// 指定生命周期，生命周期结束后自动调用 destroy， 测试过程中，可以将Lifetime = 0 使屏幕常亮
*/
static void initialize(gui_menu_t* menu, void* pvParameter)
{
    menu->interval = 0;
    menu->lifetime = 0;

    mp_lang_fling = gui_fling_get();
    gui_fling_init_t init_obj = GUI_FLING_CONFIG(0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, true, ROLL_V_MIN_OFFSET, ROLL_V_MAX_OFFSET, ITEM_HEIGHT, 100, NULL);
    gui_fling_init(mp_lang_fling, &init_obj);


    const profile_system_t* p_profile_sys = xapp_profile_get_system();
    m_idx_of_selected = p_profile_sys->langauage;
    m_idx_of_first_item = m_idx_of_selected - 2;
    mp_lang_fling->roll_offset_total = ((-m_idx_of_first_item) - 1) * ITEM_HEIGHT;
}
static void on_set_parent_func(gui_menu_t* menu, const gui_view_t* parent)
{
    //    p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t* menu)
{
    gui_fling_set_disable(mp_lang_fling);
    return GUI_DESTROY_RET_DEFAULT;
}
/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 **
*/
static void display_callback_func(gui_menu_t* menu)
{
    ui_property_t prop;
    ui_widgets_type_t* widgets = menu->widgets;

    if (IS_REQ_SRC_CREATE(menu->source))
    {
        // nothings to do ...
    }

    // 计算items 
    m_idx_of_first_item = -((mp_lang_fling->roll_offset_total / ITEM_HEIGHT) + 1 );
    gui_layout_update_offset_y(widgets, WIDGET_COUNT(wglLanguage), mp_lang_fling->item_delta, UI_WIDGET_TYPE_STACK);

    int16_t idx = m_idx_of_first_item;
    int16_t idx_of_stack = 2;

    string_t const* s;
    language_item_t const* l;
    bool check_selected = false;
    for (int i = 0; i < 5; i++)
    {
        if (idx < 0 || idx >= tagLanguageSelectCount)
        {
            widgets[idx_of_stack].visible = 0;
            idx++;
            idx_of_stack += 4;
            continue;
        }

        widgets[idx_of_stack].visible = 1;
        l = &LANGUAGE_ITEMS[idx];
        s = xapp_local_text_item_get(l->origin);
        PROP_ASSIGN_STRING(prop, s);
        PROP_UPDATE_AT_NO(idx_of_stack + 2);

        s = xapp_local_text_item_get(l->local);
        PROP_ASSIGN_STRING(prop, s);
        PROP_UPDATE_AT_NO(idx_of_stack + 3);

        if (idx == m_idx_of_selected)
        {
            check_selected = true;
            widgets[22].visible = 1;
            widgets[22].y = widgets[idx_of_stack].y + widgets[idx_of_stack].uiStack.delta_y;
        }

        idx++;
        idx_of_stack += 4;
    }
    if (!check_selected)
    {
        widgets[22].visible = 0;
    }
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组
*/
static int display_refresh_func(gui_menu_t* menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t* menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;

    switch (touch->type)
    {
    case TP_ALGO_EVT_TYPE_DOWN:
        gui_fling_on_touch_press(mp_lang_fling);
        break;
    case TP_ALGO_EVT_TYPE_UP:
        gui_fling_on_touch_release(mp_lang_fling);
        break;
    case TP_ALGO_EVT_TYPE_CLICK:
        gui_widget_list_check_touch(menu->widgets, menu->cnt_of_widgets, 0, 0, touch->x, touch->y, local_view_event_handler);
        break;
    case TP_ALGO_EVT_TYPE_HOLD:
        if (touch->deltaY != 0)
        {
            gui_fling_set_roll(mp_lang_fling, touch->deltaY);
        }
        break;
    case TP_ALGO_EVT_TYPE_SLIDE_D:
        gui_fling_switch_item(mp_lang_fling, 1, false);
        break;
    case TP_ALGO_EVT_TYPE_SLIDE_U:
        gui_fling_switch_item(mp_lang_fling, 1, true);
        break;
    }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t* menu)
{
    kb_evt_t* kb = (kb_evt_t*)pvParameters;

    switch( kb->type )
    {
        case KB_EVT_CLICK:
        local_view_event_handler( tagLanguageBack );
        break;
    }
}

static void on_event_update(void* pvParameters, gui_menu_t* menu)
{
    custom_evt_t* p_evt =  (custom_evt_t*)pvParameters;
    switch( p_evt->type )
    {
        case CUSTOM_EVT_TYPE_LANGUAGE_CHANGE:
        {
            gui_update_event(); 
            const profile_system_t* p_profile_sys = xapp_profile_get_system();
            m_idx_of_selected = p_profile_sys->langauage;
            // m_idx_of_first_item = m_idx_of_selected - 2;
            // mp_lang_fling->roll_offset_total = ((-m_idx_of_first_item) - 1) * ITEM_HEIGHT;
        }
        break;
    }
}

static void local_view_event_handler(uint32_t tag)
{
    switch (tag)
    {
        case tagLanguageItem0:
        case tagLanguageItem1:
        case tagLanguageItem2:
        case tagLanguageItem3:
        case tagLanguageItem4:
        {
            // 计算当前点击的位置 
            int idx = (int)(tag + m_idx_of_first_item);
            LOG_INFO1("[Language]: click tag = %d\r\n", idx);
            if (idx >= 0 && idx < tagLanguageSelectCount)
            {
                if( m_idx_of_selected != idx )
                {
                    m_idx_of_selected = idx;
                    xapp_profile_write_conf( PROFILE_TAG_LANGAUAGE, (uint8_t*)&idx, 1 );
                    xapp_access_LANGAUAGE_update();
                    // gui_update_manual();
                }
            }    
        }
        break;

        case tagLanguageBack:
        gui_view_create_return( &viewAppsList );
        break;
    }
}
